package com.heyou.ticket.tools;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.heyou.ticket.entity.TicketBookDetail;
import com.heyou.ticket.pojo.TicketBookVo;
import com.heyou.ticket.service.ITicketBookDetailService;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TicketBookDetailTools {

    private final ITicketBookDetailService ticketBookDetailService;

    public TicketBookDetailTools(ITicketBookDetailService ticketBookDetailService) {
        this.ticketBookDetailService = ticketBookDetailService;
    }

    // 查询所有预定信息
    @Tool(name = "查询所有预定信息", description = "查询所有预定机票的信息")
    public List<TicketBookDetail> getAllTicketBookDetails() {
        return ticketBookDetailService.lambdaQuery().eq(TicketBookDetail::getWthrDel, "0").list();
    }

    // 根据姓名和预定编号查询机票信息
    @Tool(name = "查询指定客户编号和客户姓名的机票信息", description = "根据客户姓名和客户编号查询机票信息")
    public TicketBookDetail getTicketBookDetailByNameAndNumber(@ToolParam(description = "客户姓名") String name,
                                                               @ToolParam(description = "客户编号") String number,
                                                               @ToolParam(description = "预定日期") String bookTime) {
        return ticketBookDetailService.lambdaQuery()
                .eq(TicketBookDetail::getCustomerName, name)
                .eq(TicketBookDetail::getCustomerNumb, number)
                .eq(TicketBookDetail::getBookStatus, "1")
                .likeLeft(TicketBookDetail::getBookTime, bookTime)
                .one();
    }

    @Tool(name = "新增预定机票信息", description = "根据乘客信息，新增预定机票记录。在新增预定信息前先通过getTicketBookDetailByNameAndNumber()方法查询客户是否存在有效的预定信息，若存在则不新增，并告知客户相关的预定信息，否则新增预定信息")
    public TicketBookDetail addTicketBookDetail(TicketBookVo ticketBookVo) {
        TicketBookDetail ticketBookDetail = new TicketBookDetail();
        BeanUtils.copyProperties(ticketBookVo, ticketBookDetail);
        ticketBookDetail.setCreateTime(LocalDateTime.now());
        ticketBookDetailService.save(ticketBookDetail);
        return ticketBookDetail;
    }


    // 修改预定的机票信息
    @Tool(name = "修改预定机票信息", description = "根据预定编号和乘客姓名，修改预定机票信息")
    public String updateBookStatusByNameAndNumber(TicketBookVo ticketBookVo) {
        boolean isSuccess = false;
        if (ticketBookVo.getBookId() != null) {
            TicketBookDetail detail = ticketBookDetailService.getById(ticketBookVo.getBookId());
            if (detail != null) {
                detail.setAccomType(ticketBookVo.getAccomType());
                isSuccess = ticketBookDetailService.updateById(detail);
            }
        }
        if (isSuccess) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    // 根据预定编号和乘客姓名，取消机票
    @Tool(name = "取消或退订预定机票", description = "根据预定编号和乘客姓名，取消或退订预定机票")
    public boolean cancelBookByNameAndNumber(@ToolParam(description = "预定编号") String number, @ToolParam(description = "乘客姓名") String name) {
        UpdateWrapper<TicketBookDetail> wrapper = new UpdateWrapper<>();
        wrapper.eq("customer_name", name);
        wrapper.eq("book_id", number);
        wrapper.set("book_status", "2");
        return ticketBookDetailService.update(wrapper);
    }

    // 根据预定编号和乘客姓名，取消机票
    @Tool(name = "删除预定机票", description = "根据预定编号和乘客姓名，删除预定机票")
    public boolean deleteBookByNameAndNumber(@ToolParam(description = "预定编号") String number, @ToolParam(description = "乘客姓名") String name) {
        UpdateWrapper<TicketBookDetail> wrapper = new UpdateWrapper<>();
        wrapper.eq("customer_name", name);
        wrapper.eq("book_id", number);
        wrapper.set("wthr_del", "1");
        return ticketBookDetailService.update(wrapper);
    }
}
